import Vue from 'vue';
import VueJsonp from 'vue-jsonp';

Vue.use(VueJsonp);

const Urls = {
    multiget: "https://mce.mogucdn.com/jsonp/multiget/3",
    searchlist:"https://list.mogujie.com/search",
    word:"https://list.mogujie.com/module/mget",
    typeGet:"https://mce.mogucdn.com/jsonp/get/3",

}

function error(error) {
    // console.log(error);
    window.$showTip(error.message);
}

function loadDataFormMtiget(params = {}) {
    return Vue.jsonp( Urls.multiget, params )
        .then(({ data, success }) => {
            if (success) {
                return data
            }
            return Promise.reject({
                message: "请求失败"
            });
        })
}

function loadDataforsearch(params = {}){
    return Vue.jsonp( Urls.searchlist, params )
        .then(({ result, success })=>{
            if (success) {
                return result
            }
            return Promise.reject({
                message: "请求失败"
            });
        })
}   

function loadDataTypeforget(params = {}){
    return Vue.jsonp( Urls.typeGet, params )
    .then(({ data, success })=>{
        if (success) {
            return data
        }
        return Promise.reject({
            message: "请求失败"
        });
    })
}

function loadDataFormWord(params = {}){
    return Vue.jsonp( Urls.word, params )
    .then(({ data, success })=>{
        if(success){
            return data
        }
        return Promise.reject({
            message: "请求失败"
        })
    })
}

export default {
    install: function (Vue) {
        Vue.prototype.$api = {
            loadDataFormMtiget,
            loadDataforsearch,
            loadDataTypeforget,
            loadDataFormWord
        },
        Vue.prototype.$error = error
    }
}